import turtle
import math

# 设置画布
screen = turtle.Screen()
screen.title("乡村风景")
screen.setup(width=800, height=600)
screen.bgcolor("#87CEEB")  # 天空蓝色

# 创建画笔
pen = turtle.Turtle()
pen.speed(4)  # 绘制速度
pen.hideturtle()

def draw_mountain(t, x, y, width, height, color):
    """绘制一座山"""
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.fillcolor(color)
    t.begin_fill()
    t.goto(x + width/2, y + height)
    t.goto(x + width, y)
    t.end_fill()

def draw_sun(t, x, y, radius):
    """绘制太阳"""
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.fillcolor("#FFD700")
    t.begin_fill()
    t.circle(radius)
    t.end_fill()
    
    # 绘制太阳光芒
    t.pensize(2)
    t.color("#FFD700")
    for _ in range(12):
        t.penup()
        t.goto(x, y + radius)
        t.pendown()
        t.setheading(t.heading() + 30)
        t.forward(radius // 2)

def draw_tree(t, x, y, trunk_width, trunk_height, crown_radius):
    """绘制一棵树"""
    # 树干
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.fillcolor("#8B4513")
    t.begin_fill()
    for _ in range(2):
        t.forward(trunk_width)
        t.left(90)
        t.forward(trunk_height)
        t.left(90)
    t.end_fill()
    
    # 树冠
    draw_circle(t, x + trunk_width/2, y + trunk_height + crown_radius, crown_radius, "#228B22")

def draw_circle(t, x, y, radius, color):
    """绘制圆形"""
    t.penup()
    t.goto(x, y - radius)
    t.pendown()
    t.fillcolor(color)
    t.begin_fill()
    t.circle(radius)
    t.end_fill()

def draw_house(t, x, y, width, height, roof_height):
    """绘制房屋"""
    # 房屋主体
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.fillcolor("#F0E68C")
    t.begin_fill()
    for _ in range(2):
        t.forward(width)
        t.left(90)
        t.forward(height)
        t.left(90)
    t.end_fill()
    
    # 屋顶
    t.penup()
    t.goto(x, y + height)
    t.pendown()
    t.fillcolor("#8B0000")
    t.begin_fill()
    t.forward(width)
    t.left(135)
    t.forward(math.sqrt((width/2)**2 + roof_height**2))
    t.left(90)
    t.forward(math.sqrt((width/2)**2 + roof_height**2))
    t.left(135)
    t.end_fill()
    
    # 门
    door_width = width // 5
    door_height = height // 2
    t.penup()
    t.goto(x + width/2 - door_width/2, y)
    t.pendown()
    t.fillcolor("#8B4513")
    t.begin_fill()
    for _ in range(2):
        t.forward(door_width)
        t.left(90)
        t.forward(door_height)
        t.left(90)
    t.end_fill()
    
    # 窗户
    window_size = width // 6
    # 左窗
    t.penup()
    t.goto(x + width/4 - window_size/2, y + height - window_size - 10)
    t.pendown()
    t.fillcolor("#87CEFA")
    t.begin_fill()
    for _ in range(4):
        t.forward(window_size)
        t.left(90)
    t.end_fill()
    # 右窗
    t.penup()
    t.goto(x + 3*width/4 - window_size/2, y + height - window_size - 10)
    t.pendown()
    t.fillcolor("#87CEFA")
    t.begin_fill()
    for _ in range(4):
        t.forward(window_size)
        t.left(90)
    t.end_fill()

def draw_grass(t, x, y, width, height):
    """绘制草地"""
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.fillcolor("#32CD32")
    t.begin_fill()
    t.forward(width)
    t.left(90)
    t.forward(height)
    t.left(90)
    t.forward(width)
    t.left(90)
    t.forward(height)
    t.end_fill()

# 绘制草地
draw_grass(pen, -400, -100, 800, 200)

# 绘制远山
draw_mountain(pen, -400, -100, 400, 200, "#A9A9A9")
draw_mountain(pen, 0, -100, 400, 150, "#808080")

# 绘制太阳
draw_sun(pen, 250, 200, 40)

# 绘制房屋
draw_house(pen, -100, -50, 150, 100, 50)

# 绘制树木
draw_tree(pen, 150, -50, 20, 60, 30)
draw_tree(pen, -250, -30, 15, 50, 25)
draw_tree(pen, 250, -80, 25, 70, 35)

# 完成绘制
turtle.done()
    